﻿using Core.Entitys;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ConfigureSystem
{
    /// <summary>
    /// ConfigFrame.xaml 的交互逻辑
    /// </summary>
    public partial class ConfigFrame : Window
    {
        private static readonly IEnumerable ParamTypeItems;
        static ConfigFrame()
        {
            var ParamTypes = (ParamTypes[])Enum.GetValues(typeof(ParamTypes));

            var ParamTypeItems = new KeyValuePair<string, ParamTypes>[ParamTypes.Length];

            for (int i = 0; i < ParamTypes.Length; i++)
            {
                var Item = ParamTypes[i];

                ParamTypeItems[i] = new KeyValuePair<string, Core.Entitys.ParamTypes>(Enum.GetName(typeof(ParamTypes), Item), Item);
            }

            ConfigFrame.ParamTypeItems = ParamTypeItems;
        }

        private readonly ConfigInfo ConfigItem;
        public event Action<object, ConfigInfo> Saved;

        public ConfigFrame(ConfigInfo ConfigItem)
        {
            InitializeComponent();

            Type.SelectedValuePath = "Key";
            Type.DisplayMemberPath = "Key";
            Type.SelectedValuePath = "Value";

            Type.ItemsSource = ParamTypeItems;

            var ConfigBoxItem = new ConfigInfoBox(ConfigItem);

            Type.SelectedValue = ConfigBoxItem.Type;

            if (Type.SelectedIndex == -1)
            {
                Type.SelectedIndex = 0;
            }

            Name.Text = ConfigBoxItem.Name;
            Value.Text = ConfigBoxItem.Value;
            Description.Text = ConfigBoxItem.Description;

            this.ConfigItem = ConfigItem;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            ConfigItem.Name = Name.Text;

            if (string.IsNullOrEmpty(ConfigItem.Name))
            {
                MessageBox.Show("Name can not be null.", "Operation tips");
                return;
            }

            ConfigItem.Description = Description.Text;

            if (string.IsNullOrEmpty(ConfigItem.Description))
            {
                MessageBox.Show("Description can not be null.", "Operation tips");
                return;
            }

            try
            {
                switch ((ParamTypes)Type.SelectedValue)
                {
                    case ParamTypes.Boolean:
                        ConfigItem.Value = bool.Parse(Value.Text);
                        break;
                    case ParamTypes.Int:
                        ConfigItem.Value = int.Parse(Value.Text);
                        break;
                    case ParamTypes.Long:
                        ConfigItem.Value = long.Parse(Value.Text);
                        break;
                    case ParamTypes.Float:
                        ConfigItem.Value = float.Parse(Value.Text);
                        break;
                    case ParamTypes.Double:
                        ConfigItem.Value = double.Parse(Value.Text);
                        break;
                    case ParamTypes.String:
                        ConfigItem.Value = Value.Text;
                        break;
                    case ParamTypes.DateTime:
                        ConfigItem.Value = DateTime.Parse(Value.Text);
                        break;
                    case ParamTypes.Guid:
                        ConfigItem.Value = Guid.Parse(Value.Text);
                        break;
                    default:
                        MessageBox.Show("Invalid config type.", "Operation tips");
                        return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Operation tips");
                return;
            }

            this.Saved?.Invoke(this, ConfigItem);

            this.Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}